package com.max.xihu.patterns.proxy.dynamic;

import com.max.xihu.patterns.proxy.common.Coder;
import com.max.xihu.patterns.proxy.common.IUser;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @Author: Max
 * @Date: 2020-09-13 15:15
 * @Description:
 */
public class ProxyClient {

    public static void main(String[] args) {

        IUser user = new Person("张三");
        InvocationHandler handler = new UserProxyHandler(user);
        //获取user的类加载器
        ClassLoader classLoader = user.getClass().getClassLoader();
        //动态生成代理
        IUser proxyUser  = (IUser) Proxy.newProxyInstance(classLoader, new Class[]{IUser.class}, handler);
        proxyUser.eat();
        proxyUser.work();
        proxyUser.sleep();
    }
}
